//
//  RJCityConnect.m
//  AirQuality
//
//  Created by Po on 15/3/11.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCityConnectView.h"
#import "RJCityConnectModel.h"

#import "UIImage+RJImage.h"
#import "RJLoadView.h"
#import "XYViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "RJDounghnutView.h"
#import "MBProgressHUD+MJ.h"

@interface RJCityConnectView () <RJHomeBottomFatherViewDelegate>

- (void)initData;
- (void)initInterface;
- (void)initNet;
@property (retain, nonatomic) RJDounghnutView * dounghnutView;      //环状图

@end

@implementation RJCityConnectView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
        [self initInterface];
        [self initNet];
    }
    return self;
}

#pragma mark - user-defined initialization function

- (void)initData
{
    self.type = HomeBottomTableCity;
    [self setDelegate:self];
}

- (void)initInterface
{
    UIImageView * pieBackImage = [[UIImageView alloc] initWithFrame:self.pieContentView.bounds];
    [pieBackImage setImage:GetImage(@"connectCityBack.png")];
    pieBackImage.userInteractionEnabled = YES;
    [self.pieContentView addSubview:pieBackImage];
    self.pieContentView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.pieContentView.layer.shadowOffset = CGSizeMake(0,1);
    self.pieContentView.layer.shadowOpacity = 0.6;
    self.pieContentView.layer.shadowRadius = 1.0;
}

- (void)initNet
{
    self.isConnecting = YES;
    [MBProgressHUD showMessage:@"正在加载..."];
    [RJNetWork myNetRequestWithAddress:[NetURL defaultURL].CityConnect data:@{} completeBlock:^(id responseObject) {
        //初始化数据
        if ([RJUserData defaultRJUserData].isProvince) {
            self.offLine = [[responseObject objectForKey:@"ALLOFFLINE"] integerValue];
            self.onLine = [[responseObject objectForKey:@"ALLONLINE"] integerValue];
            self.detailData = [NSArray arrayWithArray:[responseObject objectForKey:@"DATA"]];
        } else {
            self.offLine = 0;
            self.onLine = 0;
            NSMutableArray * offLineStations = [[NSMutableArray alloc] init];
            for (NSDictionary * dic in responseObject) {
                if ([dic[@"STATUS"] isEqualToString:@"在线"]) {
                    self.onLine += 1;
                } else {
                    self.offLine += 1;
                    [offLineStations addObject:dic[@"STATIONNAME"]];
                }
            }
            self.detailData = [NSArray arrayWithArray:offLineStations];
        }
        
        [self netFinish:NetRequestStatusSucceed];
        [self createPieView];
        [self freeLoadingView];
        [MBProgressHUD hideHUD];
    }failBlack:^(id response) {
        [self netFinish:NetRequestStatusFailure];
    }];
}

- (void)createPieView
{
    [self.pieContentView addSubview:[self getPieView:self.pieContentView.bounds title:@"城市实时联网"]];
    [self.pieContentView bringSubviewToFront:self.pieTitleLabel];
    [self.pieView setValueArr:@[@(self.onLine),@(self.offLine)]];
    [self.pieView setTitleArr:@[[NSString stringWithFormat:@"在线%@个",@(self.onLine)],[NSString stringWithFormat:@"离线%@个",@(self.offLine)]]];
    CGFloat width = self.frame.size.width;
    [self.pieView setTitleRect:CGRectMake(width / 2, 15, width / 2, self.bounds.size.height - 30)];
    [self.pieView setColorArr:@[[UIColor greenColor],GetRGBColor(200, 200, 200, 1)]];
    self.pieView.scaleY = 0;
    self.pieView.spaceHeight = 0;
    [self showPieViewButton];
     //设置圆环
    NSArray *dataArray = @[[NSNumber numberWithInt:self.onLine],[NSNumber numberWithInt:self.offLine]];
    NSArray *colorsArray = [NSArray arrayWithObjects:
                            [UIColor greenColor],
                            [UIColor colorWithRed:188/255.0 green:188/255.0  blue:188/255.0  alpha:1],
                            nil];
    CGRect rect;
    if (DeviceType < 5) {
        rect.size.width = self.pieView.bounds.size.width/2 - 10;
        rect.size.height = self.pieView.bounds.size.height/2 - 10;
    }else if (DeviceType >= 5 && DeviceType < 6){
        rect.size.width = self.pieView.bounds.size.width/2 - 17;
        rect.size.height = self.pieView.bounds.size.height/2 - 17;
    }else{
        rect.size.width = self.pieView.bounds.size.width/2 - 25;
        rect.size.height = self.pieView.bounds.size.height/2 - 25;
    }
    if (DeviceType >= 6) {
        rect.origin.x = self.pieView.bounds.origin.x+30;
        rect.origin.y = self.pieView.bounds.origin.y/2+60;
    }else{
        rect.origin.x = self.pieView.bounds.origin.x+30;
        rect.origin.y = self.pieView.bounds.origin.y/2+50;
    }
    _dounghnutView = [[RJDounghnutView alloc] initWithFrame:rect];
    [_dounghnutView setData:dataArray colors:colorsArray];
    [_dounghnutView buildEmpty];
    [_dounghnutView build:2];
    [self.pieContentView addSubview:_dounghnutView];

}

- (UILabel *)getCityBoxLabelWithTitle:(NSString *)title rect:(CGRect)rect
{
    UILabel * label = [[UILabel alloc] initWithFrame:rect];
    [label setText:title];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:[UIColor blackColor]];
    [label setFont:[UIFont boldSystemFontOfSize:12.0f]];
    return label;
}

- (void)freeData
{
    
}
@end
